<template>
<el-container class="home_container">
    <!-- 头部区域 --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>-->
  <el-header class="Header">
    <div>
      <img src="../../assets/img/home.jpg" class="home_img" width="50px" height="50px">
      <span>后台管理系统（测试）</span>
    </div>
    <el-button type="info" @click="OutClick">退出</el-button>
  </el-header>
  <!-- 页面主体部分  包含两个分块~~~~~~~~~~~~~~~~~~~~~~ -->
  <el-container>
    <!-- 侧边栏 -->
    <el-aside :width="isToggle ? '64px' : '200px'" class="aside">
      <div class="toggle_button" @click="ToggleClick">|||</div>
       <el-menu
      default-active="2"
      class="el-menu-vertical-demo"
      @open="handleOpen"
      @close="handleClose"
      background-color="#2A65C3"
      text-color="#fff"
      active-text-color="#ffd04b"
      unique-opened=true
      :collapse="isToggle"
      :collapse-transition="false"
      :router="true"
      :default-active="activePath"
      >
      <!-- 一级菜单 -->
      <el-submenu :index="item.id + ''" v-for="item in Menulist" :key="item.id">
        <!-- 一级菜单的模板区域 -->
        <template slot="title">
          <!-- 图标 -->
          <i class="el-icon-location" :class="IconObj[item.id]"></i>
          <!-- 文字 -->
          <span class="first_name">{{item.authName}}</span>
        </template>
        <!-- 二级菜单 -->
          <el-menu-item 
          :index="'/'+ subitem.path + ''" 
          v-for="subitem in item.children" 
          :key="subitem.id"
          @click="saveNavState('/'+ subitem.path)">
          <template slot="title">
          <!--二级 图标 -->
          <i class="el-icon-menu"></i>
          <!--二级 文字 -->
          <span>{{subitem.authName}}</span>
        </template>
          </el-menu-item>
      </el-submenu>

    </el-menu>
    </el-aside>
    <!-- 右侧栏 `````````````内容区域``````````````-->
    <el-main class="main">
      <router-view></router-view>
    </el-main>
  </el-container>
</el-container>
</template>

<script>
export default {
  name:'home',
  data(){
    return {
      Menulist:[],
      IconObj:{
        '125':'iconfont icon-user',
        '103':'iconfont icon-tijikongjian',
        '101':'iconfont icon-shangpin',
        '102':'iconfont icon-danju',
        '145':'iconfont icon-baobiao'
     },
     isToggle:false,
     activePath:''
    }
  },
  created(){
    this.getMenulist()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods:{
    OutClick(){
      window.sessionStorage.clear()
      this.$router.push('./login')
    },
    // 获取右侧菜单数据
    async getMenulist(){
      const {data:res} = await this.$http.get('menus')
      if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.Menulist = res.data
      console.log(res)
    },
    // 点击按钮 切换菜单的折叠与展开
    ToggleClick(){
      this.isToggle = !this.isToggle
    },
    // 保存链接的激活状态
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath)
    }
  }
}
</script>

<style>
  @import '../../assets/css/home.css';
  .Header {
  background-color: #2A65C3;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
}
.Header div {
  display: flex;
  align-items: center;
}
.Header div span {
  margin-left: 15px;
}
.aside {
  background-color: #2A65C3;
}
.main {
  background-color: #eaedf1;
}
.home_container {
  height: 100%;
}
.home_img {
  width: 50px;
  height: 50px;
  border-radius: 50%;
}
.el-menu {
  border-right: none;
}
.toggle_button{
  background-color: #4A5064;
  font-size: 10px;
  line-height: 24px;
  color: white;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
  /* transition: 0.6s; */
}

</style>


